package com.woniuxy.consumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.woniuxy.commons.entity.Goods;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yangtao
 * @date 2021/8/2 14:38
 * @Version 1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private RestTemplate restTemplate;
    private String url="http://PROVIDER/goods/";

    //restTemplate+微服务名字  默认为集群方式访问
    //此时需要在config类中开启负载均衡

    @HystrixCommand(fallbackMethod = "abc")
    @GetMapping("/all")
    public List<Goods> findOrderById(){
        List forObject = restTemplate.getForObject(url+"all", List.class);
        return forObject;
    }
    public List<Goods> abc(){
        ArrayList<Goods> goods = new ArrayList<>();
        return goods;
    }

    @GetMapping("/find")
    public Goods find(){
        Goods forObject = restTemplate.getForObject(url + "find/1004", Goods.class);
        return forObject;
    }
    @GetMapping("/add")
    public boolean add(){
        Goods keyboard = new Goods(1005, "keyboard", 1000);
        //通过post请求传递对象
        //参数1：提交的数据   参数2：返回值的类型
        //传递参数为json格式字符串
        Boolean result = restTemplate.postForObject(url + "add", keyboard, boolean.class);
        return result;
    }

    @GetMapping("/update")
    public boolean update(){
        Goods keyboard = new Goods(1006, "耳机", 1000);
        //通过put请求传递对象
        //参数1：提交的数据   参数2：返回值的类型
        //传递参数为json格式字符串
        restTemplate.put(url + "update", keyboard);
        return true;
    }

    @GetMapping("/del")
    public void del(){
        restTemplate.delete(url+"del/1007");
    }
}













